Cargo librerias

library(readr)       # Leer los datos
library(tibble)      # Transformar en tibble
library(dplyr)       # Piping
library(tidyr)       # Tidy data
library(lubridate)   # Separar fecha
library(stringr)     # Manipular fechas
library(ggplot2)     # Visualización
library(plotly)      # Visualización interactiva
library(DT)          # Tabla interactiva
library(gghighlight) # Destacar caso

Cargo data

tb <- as.tibble(read.csv("data/dataset_viajes_sube.csv"))

Tidy data

tb <- tb %>% 
  rename(transporte = ï..TIPO_TRANSPORTE,
         dia = DIA,
         parcial = PARCIAL, 
         cantidad = CANTIDAD
  )

tb$dia <- tb$dia %>% 
  str_replace(":", "/") 

tb <- tb %>%
  separate(col = "dia", into = c("fecha", "hora"), sep = "/", convert = TRUE) 

tb$fecha <- dmy(tb$fecha)

tb$fecha <- tb$fecha %>% 
  str_replace("-", "/") 

tb <- tb %>%
  separate(col = "fecha", into = c("año", "mes"), sep = "/", convert = TRUE) %>% 
  separate(col = "mes", into = c("mes", "dia"), sep = "-", convert = TRUE)

tb <- tb %>% 
  select(transporte, año, mes, dia, parcial, cantidad) %>% 
  arrange(mes, dia)

Dplyr

¿En qué días hubo el mayor número de viajes?

tb %>% 
  arrange(desc(cantidad))
## # A tibble: 914 x 6
##    transporte   año   mes   dia parcial cantidad
##    <chr>      <int> <int> <int> <chr>      <int>
##  1 Colectivo   2020     3     6 False    4978911
##  2 Colectivo   2020     3    12 False    4974953
##  3 Colectivo   2020     3    10 False    4955629
##  4 Colectivo   2020     3    13 False    4923725
##  5 Colectivo   2020     3     5 False    4821868
##  6 Colectivo   2020     3     9 False    4791752
##  7 Colectivo   2020     3     4 False    4707871
##  8 Colectivo   2020     3     3 False    4678454
##  9 Colectivo   2020     3     2 False    4560574
## 10 Colectivo   2020     3    11 False    4098759
## # ... with 904 more rows

Visualizando

Quito notación científica

options(scipen = 999)

¿En qué transporte se viaja más?

tb %>% 
  ggplot(aes(x = transporte, y = cantidad)) +
  geom_col(fill = "red") +
  gghighlight(transporte == "Colectivo")

¿En qué meses hubo mayor cantidad de viajes por transporte?

tb %>% 
  ggplot(aes(x = mes)) +
  geom_line(aes(y = cantidad), size = 1.5, color = "gold") +
  geom_point(aes(y = cantidad), shape = 21, size = 4, stroke = 1, color = "black", fill = "gold") +  
  scale_x_continuous(breaks = seq(3, 12, 3), 
                     limits=c(3, 12)) +
  facet_wrap(~ transporte)

¿Cómo fluctuaron los viajes en el mes (y días) de marzo?

tb %>% 
  filter(mes == 3) %>% 
  ggplot(aes(x = dia)) +
  geom_line(aes(y = cantidad), size = 1.5, color = "gold") +
  geom_point(aes(y = cantidad), shape = 21, size = 4, stroke = 1, color = "black", fill = "gold") + 
  facet_wrap(~ transporte)

En Plotly

p <- tb %>% 
  filter(mes == 3) %>% 
  ggplot(aes(x = dia)) +
  geom_line(aes(y = cantidad), size = 1.5, color = "gold") +
  geom_point(aes(y = cantidad), shape = 21, size = 4, stroke = 1, color = "black", fill = "gold") + 
  facet_wrap(~ transporte) +
  theme(axis.title.y = element_text(angle = 0))

ggplotly(p)

¿Cómo fluctuaron los viajes desde marzo hasta ahora en lo relativo al uso del colectivo?

tb %>% 
  filter(transporte == "Colectivo") %>% 
  ggplot(aes(x = mes)) +
  geom_line(aes(y = cantidad), size = 1.5, color = "gold") +
  geom_point(aes(y = cantidad), shape = 21, size = 4, stroke = 1, color = "black", fill = "gold") +
  scale_x_continuous(breaks = seq(3, 12, 1), 
                     limits=c(3, 12)) +
  labs(title = "¿Cómo fluctuaron los viajes desde marzo hasta ahora en lo 
relativo al uso del colectivo?")

Plotly

tb %>% 
  filter(transporte == "Colectivo") %>% 
  plot_ly(x =  ~ mes, y =  ~ cantidad) %>%
  add_markers()

Tabla interactiva con el dataset completo

tb %>% 
  datatable()